package com.dcm.groupbuy.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.groupbuy.entity.GroupBuyEntity;
import com.dcm.groupbuy.mapper.GroupBuyMapper;
import com.dcm.groupbuy.service.GroupBuyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 牛浩
 */
@Service
public class GroupBuyServiceImpl implements GroupBuyService {
    @Autowired
    private GroupBuyMapper groupBuyMapper;

    @Override
    public List<GroupBuyEntity> findAll() {
        return groupBuyMapper.findAll();
    }

    @Override
    public void delete(Long id) {
        if (id == null){
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_ID_CANNOT_NULL);
        }
        groupBuyMapper.delete(id);
    }

    @Override
    public void insertGroupBuy(GroupBuyEntity groupBuyEntity) throws MyException{
        checkGroupBuy(groupBuyEntity);
        String groupName = groupBuyEntity.groupName;
        int size = 100;
        if (groupName.length()>=size){
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_NAME_SIZE_CANNOT_NULL);
        }
        for (int i = 0; i < groupName.length(); i++) {
            char c = groupName.charAt(i);
            String letter = ".*[a-zA-Z0-9]+.*";
            String chinese = "^[\u4E00-\u9FA5]+$";
            Matcher m=Pattern.compile(letter).matcher(c+"");
            Matcher m1=Pattern.compile(chinese).matcher(c+"");
            if (m.matches()==false && m1.matches()==false){
                throw new MyException(Constants.GroupBuyErrorMsg.GROUP_NAME_REGULAR_CANNOT_NULL);
            }
        }
        groupBuyMapper.insertGroupBuy(groupBuyEntity);
    }

    @Override
    public void updateGroupBuy(GroupBuyEntity groupBuyEntity) {
        groupBuyMapper.updateGroupBuy(groupBuyEntity);
    }

    @Override
    public List<GroupBuyEntity> search(Integer groupState, String groupName) {
        if (groupState == null){
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_STATE_CANNOT_NULL);
        }
        if (groupName == null){
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_NAME_CANNOT_NULL);
        }
        return groupBuyMapper.search(groupState,groupName);
    }

    @Override
    public PageInfo<GroupBuyEntity> findPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<GroupBuyEntity> list = groupBuyMapper.findAll();
        PageInfo<GroupBuyEntity> page = new PageInfo<>(list);
        return page;
    }

    private static void checkGroupBuy(GroupBuyEntity groupBuyEntity)  {
        if (groupBuyEntity.groupImg == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_IMG_CANNOT_NULL);
        }
        if (groupBuyEntity.groupName == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_NAME_CANNOT_NULL);
        }
        if (groupBuyEntity.price == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.PRICE_CANNOT_NULL);
        }
        if (groupBuyEntity.groupPrice == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_PRICE_CANNOT_NULL);
        }
        if (groupBuyEntity.groupPeople == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_PEOPLE_CANNOT_NULL);
        }
        if (groupBuyEntity.participatePeople == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.PARTICIPATE_PEOPLE_CANNOT_NULL);
        }
        if (groupBuyEntity.groupNum == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_NUM_CANNOT_NULL);
        }
        if (groupBuyEntity.limits == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.LIMITS_CANNOT_NULL);
        }
        if (groupBuyEntity.limitsSurplus == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.LIMITS_SURPLUS_CANNOT_NULL);
        }
        if (groupBuyEntity.startDate == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.START_DATE_CANNOT_NULL);
        }
        if (groupBuyEntity.endDate == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.END_DATE_CANNOT_NULL);
        }
        if (groupBuyEntity.groupState == null) {
            throw new MyException(Constants.GroupBuyErrorMsg.GROUP_STATE_CANNOT_NULL);
        }
    }
}